home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / sgi2fax / hipass.c next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  183 lines

  1. /*    $Header: /usr/people/sam/fax/sgi2fax/RCS/hipass.c,v 1.7 1994/02/28 14:23:08 sam Rel $
  2. /*
  3.  * Copyright (c) 1990, 1991, 1992, 1993, 1994 Sam Leffler
  4.  * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25. /*
  26.  *      hipass -
  27.  *              Hi pass filter rows using a 3x3 kernel.
  28.  *
  29.  *                              Paul Haeberli - 1989
  30.  *
  31.  */
  32. #include "stdio.h"
  33. #include "hipass.h"
  34.  
  35. highpass *newhp(getfunc,xsize,ysize,mag)
  36. int (*getfunc)();
  37. int xsize, ysize;
  38. float mag;
  39. {
  40.     highpass *hp; 
  41.     int i;
  42.  
  43.     hp = (highpass *)malloc(sizeof(highpass));
  44.     hp->getfunc = getfunc;
  45.     hp->xsize = xsize;
  46.     hp->ysize = ysize;
  47.     hp->y = 0;
  48.     hp->extrapval = 1024*mag;
  49.     for(i=0; i<3; i++)
  50.         hp->blurrows[i] = (short *)malloc(xsize*sizeof(short));
  51.     for(i=0; i<2; i++)
  52.         hp->pastrows[i] = (short *)malloc(xsize*sizeof(short));
  53.     hp->acc = (short *)malloc(xsize*sizeof(short));
  54.     if(xsize<3 || ysize<3 || hp->extrapval == 0) 
  55.         hp->active = 0;
  56.     else {
  57.         hp->active = 1;
  58.     }
  59.     return hp;
  60. }
  61.  
  62. freehp(hp)
  63. highpass *hp; 
  64. {
  65.     int i;
  66.  
  67.     for(i=0; i<3; i++)
  68.         free(hp->blurrows[i]);
  69.     for(i=0; i<2; i++)
  70.         free(hp->pastrows[i]);
  71.     free(hp->acc);
  72.     free(hp);
  73. }
  74.  
  75. /*
  76.  *      extrap -
  77.  *              Extrapolate from the blurred image beyond the original
  78.  *
  79.  */
  80. static extrap(pix,acc,div,n,extrapval)
  81. unsigned short *pix;
  82. short *acc;
  83. int div, n;
  84. int extrapval;
  85. {
  86.     int delta, val;
  87.     int mul, ext;
  88.  
  89.     mul = div;
  90.     div = div*256;
  91.     ext = extrapval;
  92.     while(n--) {
  93.         delta = (*pix * mul)-*acc++;
  94.         val = *pix+(ext*delta)/div;
  95.         if(val<0)
  96.             val = 0;
  97.         if(val>255)
  98.             val = 255;
  99.         *pix++ = val;
  100.     }
  101. }
  102.  
  103. /*
  104.  *      xblur -
  105.  *              Blur a row in the x direction
  106.  *
  107.  */
  108. static xblur(sptr,cptr,n)
  109. unsigned short *sptr, *cptr;
  110. int n;
  111. {
  112.     short acc;
  113.  
  114.     if (n<3) {
  115.         fprintf(stderr,"xblur: n may not be less than 3\n");
  116.         exit(1);
  117.     }
  118.     acc = cptr[0] + cptr[1];
  119.     *sptr++ = (3*acc+1)>>1;
  120.     acc += cptr[2];
  121.     n-=3;
  122.     while(n--) {
  123.         *sptr++ = acc;
  124.         acc -= cptr[0];
  125.         acc += cptr[3];
  126.         cptr++;
  127.     }
  128.     *sptr++ = acc;
  129.     acc -= cptr[0];
  130.     *sptr++ = (3*acc+1)>>1;
  131. }
  132.  
  133. hpgetrow(hp,buf,y)
  134. highpass *hp;
  135. short *buf;
  136. int y;
  137. {
  138.     short *temp;
  139.     int iy;
  140.  
  141.     if(y == 0)
  142.         hp->y = 0;
  143.     if(hp->y != y) {
  144.         fprintf(stderr,"hpgetrow: y error\n");
  145.         exit(1);
  146.     }
  147.     if(hp->active) {
  148.         if (y == 0) {
  149.             memset(hp->acc,0,hp->xsize*sizeof(short));
  150.             for(iy=0; iy<2; iy++) {
  151.                 temp = hp->pastrows[0];
  152.                 hp->pastrows[0] = hp->pastrows[1];
  153.                 hp->pastrows[1] = temp;
  154.                 hp->getfunc(hp->pastrows[1],iy);
  155.                 xblur(hp->blurrows[iy],hp->pastrows[1],hp->xsize);
  156.                 addsrow(hp->acc,hp->blurrows[iy],hp->xsize);
  157.             }
  158.             copyrow(hp->pastrows[0],buf,hp->xsize);
  159.             extrap(buf,hp->acc,6,hp->xsize,hp->extrapval);
  160.         } else if (y == hp->ysize-1) {
  161.             copyrow(hp->pastrows[1],buf,hp->xsize);
  162.             extrap(buf,hp->acc,6,hp->xsize,hp->extrapval);
  163.         } else {
  164.             temp = hp->pastrows[0];
  165.             hp->pastrows[0] = hp->pastrows[1];
  166.             hp->pastrows[1] = temp;
  167.             hp->getfunc(hp->pastrows[1],y+1);
  168.             xblur(hp->blurrows[2],hp->pastrows[1],hp->xsize);
  169.             addsrow(hp->acc,hp->blurrows[2],hp->xsize);
  170.             copyrow(hp->pastrows[0],buf,hp->xsize);
  171.             extrap(buf,hp->acc,9,hp->xsize,hp->extrapval);
  172.             subsrow(hp->acc,hp->blurrows[0],hp->xsize);
  173.             temp = hp->blurrows[0];
  174.             hp->blurrows[0] = hp->blurrows[1];
  175.             hp->blurrows[1] = hp->blurrows[2];
  176.             hp->blurrows[2] = temp;
  177.         }
  178.     } else {
  179.         hp->getfunc(buf,y);
  180.     }
  181.     hp->y++;
  182. }
  183.